#include "main.h"
#include "gpio.h"
#include "tim.h"
#include "Hardware_Motor.h"

/* 基础全局变量 *****************/
Motor_t LF_Wheel;
Motor_t RF_Wheel;
Motor_t LB_Wheel;
Motor_t RB_Wheel;
/***************** 基础全局变量 */

/* 功能函数 *********************/

/**
 * @brief 获得电机指针
 * 
 * @return Motor* 
 */
Motor_t* Motor_GetMotorPtr_LF(void)
{
    return &LF_Wheel;
}
Motor_t* Motor_GetMotorPtr_RF(void)
{
    return &RF_Wheel;
}
Motor_t* Motor_GetMotorPtr_LB(void)
{
    return &LB_Wheel;
}
Motor_t* Motor_GetMotorPtr_RB(void)
{
    return &RB_Wheel;
}

/**
 * @brief 电机初始化
 * 
 */
void Motor_Init(void)
{
    /* 初始化变量 */
    LF_Wheel.OutputPWM = 0;
    RF_Wheel.OutputPWM = 0;
    LB_Wheel.OutputPWM = 0;
    RB_Wheel.OutputPWM = 0;

    LF_Wheel.Direction = 0;
    RF_Wheel.Direction = 0;
    LB_Wheel.Direction = 0;
    RB_Wheel.Direction = 0;

    /* 设置引脚 */
    LF_Wheel.usedTIM = MOTOR_LF_USEDTIM;
    LF_Wheel.usedChannel = MOTOR_LF_USEDCHANNEL;
    LF_Wheel.usedGPIO1 = MOTOR_LF_USEDGPIO1;
    LF_Wheel.usedGPIO2 = MOTOR_LF_USEDGPIO2;
    LF_Wheel.usedGPIO1Pin = MOTOR_LF_USEDGPIO1PIN;
    LF_Wheel.usedGPIO2Pin = MOTOR_LF_USEDGPIO2PIN;

    RF_Wheel.usedTIM = MOTOR_RF_USEDTIM;
    RF_Wheel.usedChannel = MOTOR_RF_USEDCHANNEL;
    RF_Wheel.usedGPIO1 = MOTOR_RF_USEDGPIO1;
    RF_Wheel.usedGPIO2 = MOTOR_RF_USEDGPIO2;
    RF_Wheel.usedGPIO1Pin = MOTOR_RF_USEDGPIO1PIN;
    RF_Wheel.usedGPIO2Pin = MOTOR_RF_USEDGPIO2PIN;

    LB_Wheel.usedTIM = MOTOR_LB_USEDTIM;
    LB_Wheel.usedChannel = MOTOR_LB_USEDCHANNEL;
    LB_Wheel.usedGPIO1 = MOTOR_LB_USEDGPIO1;
    LB_Wheel.usedGPIO2 = MOTOR_LB_USEDGPIO2;
    LB_Wheel.usedGPIO1Pin = MOTOR_LB_USEDGPIO1PIN;
    LB_Wheel.usedGPIO2Pin = MOTOR_LB_USEDGPIO2PIN;

    RB_Wheel.usedTIM = MOTOR_RB_USEDTIM;
    RB_Wheel.usedChannel = MOTOR_RB_USEDCHANNEL;
    RB_Wheel.usedGPIO1 = MOTOR_RB_USEDGPIO1;
    RB_Wheel.usedGPIO2 = MOTOR_RB_USEDGPIO2;
    RB_Wheel.usedGPIO1Pin = MOTOR_RB_USEDGPIO1PIN;
    RB_Wheel.usedGPIO2Pin = MOTOR_RB_USEDGPIO2PIN;
    
    /* 启动PWM */
    HAL_TIM_PWM_Start(LF_Wheel.usedTIM, LF_Wheel.usedChannel);
    HAL_TIM_PWM_Start(RF_Wheel.usedTIM, RF_Wheel.usedChannel);
    HAL_TIM_PWM_Start(LB_Wheel.usedTIM, LB_Wheel.usedChannel);
    HAL_TIM_PWM_Start(RB_Wheel.usedTIM, RB_Wheel.usedChannel);
}

/**
 * @brief 设置电机方向，可以使用该函数，也允许直接更改Wheel_t结构体中的Direction变量
 *
 * @param wheel
 * @param direction
 */
void Motor_SetDriction(Motor_t *wheel, int8_t direction)
{
    wheel->Direction = direction; // 更新方向
    if (direction == 1)
    {
        HAL_GPIO_WritePin(wheel->usedGPIO1, wheel->usedGPIO1Pin, GPIO_PIN_SET);
        HAL_GPIO_WritePin(wheel->usedGPIO2, wheel->usedGPIO2Pin, GPIO_PIN_RESET);
    }
    else if (direction == -1)
    {
        HAL_GPIO_WritePin(wheel->usedGPIO1, wheel->usedGPIO1Pin, GPIO_PIN_RESET);
        HAL_GPIO_WritePin(wheel->usedGPIO2, wheel->usedGPIO2Pin, GPIO_PIN_SET);
    }
    else
    {
        HAL_GPIO_WritePin(wheel->usedGPIO1, wheel->usedGPIO1Pin, GPIO_PIN_SET);
        HAL_GPIO_WritePin(wheel->usedGPIO2, wheel->usedGPIO2Pin, GPIO_PIN_SET);
    }
}

/**
 * @brief 设置电机PWM，可以使用该函数，也允许直接更改Wheel_t结构体中的OutputPWM变量
 *
 * @param wheel
 * @param pwm
 */
void Motor_SetPWM(Motor_t *wheel, uint32_t pwm)
{
    if (pwm > 9999)
    {
        pwm = 9999;
    }
    wheel->OutputPWM = pwm; // 更新PWM值

    __HAL_TIM_SET_COMPARE(wheel->usedTIM, wheel->usedChannel, pwm);
}

/**
 * @brief 更新电机状态
 * 
 */
void Motor_Update(void)
{
    Motor_SetDriction(&LF_Wheel, LF_Wheel.Direction);
    Motor_SetPWM(&LF_Wheel, LF_Wheel.OutputPWM);
    Motor_SetDriction(&RF_Wheel, RF_Wheel.Direction);
    Motor_SetPWM(&RF_Wheel, RF_Wheel.OutputPWM);
    Motor_SetDriction(&LB_Wheel, LB_Wheel.Direction);
    Motor_SetPWM(&LB_Wheel, LB_Wheel.OutputPWM);
    Motor_SetDriction(&RB_Wheel, RB_Wheel.Direction);
    Motor_SetPWM(&RB_Wheel, RB_Wheel.OutputPWM);
}
